﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading.Tasks;
using Peter.Common;
using Peter.Common.Interfaces;
using Peter.Common.MainMenu;

namespace Peter.Logic
{
   /// <summary>
   /// A quick open provider for files.
   /// </summary>
   [Export (typeof (IQuickOpenProvider))]
   public class FileSystemQuickOpenProvider : IQuickOpenProvider
   {
      /// <summary>
      /// Gets the title of the quick open provider.
      /// </summary>
      public string Title
      {
         get { return "File System"; }
      }

      /// <summary>
      /// Opens the quick open item with the given key.
      /// </summary>
      /// <param name="item">The quick open item to...open...</param>
      /// <returns>The results of the quick open.</returns>
      public QuickOpenResults Open (QuickOpenItem item)
      {
         var dir = item.Tag as DirectoryInfo;
         if (dir != null)
            return new QuickOpenResults (QuickOpenResultType.ReplaceText) { ReplaceText = dir.FullName + "\\" };
         var file = item.Tag as FileInfo;
         if (file != null)
            return new QuickOpenResults (QuickOpenResultType.OpenInPeter) { FileToOpen = file.FullName };
         return new QuickOpenResults (QuickOpenResultType.Finished);
      }

      /// <summary>
      /// Gets a dictionary of quick open items for quick open to use.
      /// </summary>
      /// <param name="filter">The Quick open filter.</param>
      /// <returns>A dictionary of items.</returns>
      public async Task<IEnumerable<QuickOpenItem>> GetQuickOpenItemsAsync (string filter)
      {
         var items = new Collection <QuickOpenItem> ();
         var currentPath = filter.Replace ('/', '\\').Replace ("\"", "");
         var currentDir = Directory.GetCurrentDirectory ();
         var file = filter;
         if (currentPath.Contains ("\\"))
         {
            var dir = currentPath.Substring (0, currentPath.LastIndexOf ('\\') + 1);
            if (Directory.Exists (dir)) currentDir = dir;
            file = currentPath.Substring (currentPath.LastIndexOf ('\\') + 1);
         }
         file = "*" + CommonUtilities.MakeValidFileName (file) + "*";
         var dirInfo = new DirectoryInfo (currentDir);
         await Task.Run (() =>
         {
            var directories = dirInfo.GetDirectories (file, SearchOption.TopDirectoryOnly);
            foreach (var dir in directories)
            {
               items.Add (new QuickOpenItem (this)
               {
                  Key = dir.FullName,
                  Text = dir.Name,
                  Tag = dir,
                  Icon = MainMenuIcon.FolderClose
               });
            }
         });
         await Task.Run (() =>
         {
            var files = dirInfo.GetFiles (file, SearchOption.TopDirectoryOnly);
            foreach (var f in files)
            {
               items.Add (new QuickOpenItem (this)
               {
                  Key = f.FullName,
                  Text = f.Name,
                  Tag = f,
                  Icon = MainMenuIconConverter.GetIconForFile (f)
               });
            }
         });
         return items;
      }
   }
}
